//
//  EnjoyDetailViewController.m
//  FreeReading
//
//  Created by lanou3g on 15/10/28.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "EnjoyDetailViewController.h"
#import "ZFPopupMenu.h"
#import "ZFPopupMenuItem.h"
#import "EGORefreshTableHeaderView.h"
#import "SRRefreshView.h"

@interface EnjoyDetailViewController ()<UIWebViewDelegate, EGORefreshTableHeaderDelegate, UIScrollViewDelegate>

@property(nonatomic,strong) EGORefreshTableHeaderView *m_refreshDownView;


@end

@implementation EnjoyDetailViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 7.0) {
        
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, self.view.bounds.size.height)];
    self.webView.delegate = self;
    self.webView.backgroundColor = [UIColor whiteColor];
    self.webView.scrollView.delegate = self;
    self.webView.opaque = NO;
    [self.view addSubview:self.webView];
    self.navigationController.navigationBar.translucent = YES;
   [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url]]];
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(popViewController:)];
    swipe.direction = UISwipeGestureRecognizerDirectionRight;
    [self.webView addGestureRecognizer:swipe];
    
    [self setSubviews];
}

#pragma mark - 轻扫返回
- (void)popViewController:(UISwipeGestureRecognizer *)swipe
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)addWebViewFooter
{
    //上拉刷新
    EGORefreshTableHeaderView *tempview=[[EGORefreshTableHeaderView alloc] initWithFrame:
                                         CGRectMake(0, self.webView.scrollView.contentSize.height, kScreenWidth, 44)];
    tempview.delegate=self;
    
    self.m_refreshDownView = tempview;
    [self.webView.scrollView addSubview:self.m_refreshDownView];
   
}
#pragma mark - webview上拉回调
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self.m_refreshDownView egoRefreshScrollViewDidEndDragging:self.webView.scrollView];
}
- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view
{
    [HUDHeaper addHUDInView:self.view text:@"页面即将消失" hideAfterDelay:0.1];
    [self performSelector:@selector(FinishedRefresh) withObject:nil afterDelay:0.2];
}

-(void)FinishedRefresh
{
    [self.m_refreshDownView egoRefreshScrollViewDataSourceDidFinishedLoading:self.webView.scrollView];
    
    [UIView  beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.75];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.navigationController.view cache:NO];
    [UIView commitAnimations];
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDelay:0.375];
    [self.navigationController popViewControllerAnimated:NO];
    [UIView commitAnimations];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
// 下拉
- (NSArray *)menuItems
{
    
    ZFPopupMenuItem *item1 = [ZFPopupMenuItem initWithMenuName:@"分享" image:[UIImage imageNamed:@"iconfont-fenxiang"] action:@selector(button1:) target:self];
    
    ZFPopupMenuItem *item2 = [ZFPopupMenuItem initWithMenuName:@"收藏" image:[UIImage imageNamed:@"iconfont-shoucang"] action:@selector(button2:) target:self];
    
    return @[item1, item2];
}
- (void)setSubviews
{
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
   UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = self.newsTitle;
    titleLabel.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = titleLabel;
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-icon_back"] style:(UIBarButtonItemStylePlain) target:self action:@selector(back:)];
    self.navigationItem.leftBarButtonItem = leftButton;
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-tubiao13"] style:(UIBarButtonItemStylePlain) target:self action:@selector(share:)];
    
    self.navigationItem.rightBarButtonItem = rightItem;
}

#pragma mark - 导航条button点击事件
- (void)back:(UIBarButtonItem *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)share:(UIBarButtonItem *)button
{
    ZFPopupMenu *popupMenu = [[ZFPopupMenu alloc] initWithItems:[self menuItems]];
    [popupMenu showInView:self.navigationController.view fromRect:( CGRectMake(kScreenWidth - 30, -44, 100, 100))  layoutType:Vertical];
    [self.navigationController.view addSubview:popupMenu];
    
}

- (void)button1:(UIButton *)share
{
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"免责声明" message:@"1、一切移动客户端用户在下载并浏览本APP软件时均被视为已经仔细阅读本条款并完全同意。凡以任何方式登陆本APP，或直接、间接使用本APP资料者，均被视为自愿接受本网站相关声明和用户服务协议的约束。\n2、本APP转载的内容并不代表本APP之意见及观点，也不意味着本网赞同其观点或证实其内容的真实性。\n3、本APP转载的文字、图片、音视频等资料均由本APP用户提供，其真实性、准确性和合法性由信息发布人负责。本APP不提供任何保证，并不承担任何法律责任。\n4、本APP所转载的文字、图片、音视频等资料，如果侵犯了第三方的知识产权或其他权利，责任由作者或转载者本人承担，本APP对此不承担责任。 \n5、本APP不保证为向用户提供便利而设置的外部链接的准确性和完整性，同时，对于该外部链接指向的不由本APP实际控制的任何网页上的内容，本APP不承担任何责任。。" preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"知道了" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.pic_url]];
        
        [UMSocialSnsService presentSnsIconSheetView:self appKey:@"5634903d67e58ecc6300439e" shareText:[NSString stringWithFormat:@"%@@%@", self.newsTitle, self.url] shareImage:[UIImage imageWithData:data] shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina, UMShareToTencent, UMShareToRenren, UMShareToDouban, UMShareToWechatTimeline, UMShareToWechatSession, nil] delegate:nil];
        
        [UMSocialData defaultData].extConfig.wechatSessionData.url = self.url;
        [UMSocialData defaultData].extConfig.wechatTimelineData.url = self.url;

        
        
    }];
    
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
    
}

- (void)button2:(UIButton *)collect
{
    
    if ([[CollectManager shareManager] selectDatabaseWithTitle:self.newsTitle].count > 0) {
        
        [HUDHeaper addHUDInView:self.view text:@"已经收藏过" hideAfterDelay:1.0];
    } else {
        
        [[CollectManager shareManager] insertDatabaseWithTitle:self.newsTitle picUrl:self.pic_url webUrl:self.url tag:self.tag];
        
        [HUDHeaper addHUDInView:self.view text:@"收藏成功" hideAfterDelay:1.0];
        
    }
    
    
    
    
}



// 取消页面单击
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    //判断是否是单击
    if (navigationType == UIWebViewNavigationTypeLinkClicked)
    {
        return NO;
    }
    return YES;
}
-(void)webViewDidStartLoad:(UIWebView *)webView
{
    [HUDHeaper addHUDInView:self.view text:@"加载中..."];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    
    [HUDHeaper hideAllHUDsForView:self.view animated:YES];
    
    // 取消连接
      [webView stringByEvaluatingJavaScriptFromString:@"$(document).ready(function () {$(\"a\").removeAttr(\"href\");})"];
    NSString *tempString2 = [NSString stringWithFormat:@"document.getElementById('downTips').innerHTML ='';"];
    [webView stringByEvaluatingJavaScriptFromString:tempString2];
    [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"var testa = document.documentElement.getElementsByClassName('CLASS_download_app_btn')[0];""testa.parentNode.removeChild(testa)"]];
  [self.webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.getElementsByClassName('icon-font-origin-btn')[0].style.height = 0"];
    // 删除评论框
     [self.webView stringByEvaluatingJavaScriptFromString:@"var testa = document.documentElement.getElementsByClassName('comment-input-box')[0];""testa.parentNode.removeChild(testa)"];
    
    [self.webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.getElementsByClassName('cb')[0].style.height = 0"];
    
    // 文章下面广告
    [self.webView stringByEvaluatingJavaScriptFromString:@"var testa = document.getElementById('article_bottom');"
     "testa.parentNode.removeChild(testa)"];
    
        // 禁用用户选择
        [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];
    
        // 禁用长按弹出框
        [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"];
  // 去掉点赞图片
    [self.webView stringByEvaluatingJavaScriptFromString:@"var testa = document.documentElement.getElementsByClassName('share-box')[0];""testa.parentNode.removeChild(testa)"];
    // 去掉评论
    for (int i = 0; i < 10; i++) {
        [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"var testa = document.documentElement.getElementsByClassName('common_top')[%d];""testa.parentNode.removeChild(testa)", i]];

        [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"var testa = document.documentElement.getElementsByClassName('c pr')[%d];""testa.parentNode.removeChild(testa)", i]];
        [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"var testa = document.documentElement.getElementsByClassName('tx')[%d];""testa.parentNode.removeChild(testa)", i]];
        [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"var testa = document.documentElement.getElementsByClassName('c pr none')[%d];""testa.parentNode.removeChild(testa)", i]];
        [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"var testa = document.documentElement.getElementsByClassName('like_box icon-font')[%d];""testa.parentNode.removeChild(testa)", i]];
        [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"var testa = document.documentElement.getElementsByClassName('tx_r')[%d];""testa.parentNode.removeChild(testa)", i]];
        [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"var testa = document.documentElement.getElementsByClassName('detial_btn_container')[%d];""testa.parentNode.removeChild(testa)", i]];
    }
    // 去掉下面新闻连接
    [self.webView stringByEvaluatingJavaScriptFromString:@"var testa = document.getElementById('top5');"
     "testa.parentNode.removeChild(testa)"];
    // 去掉选座按钮
    [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"var testa = document.documentElement.getElementsByClassName('bottom_btn')[0];""testa.parentNode.removeChild(testa)"]];
    [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"var testa = document.documentElement.getElementsByClassName('zaker_down')[0];""testa.parentNode.removeChild(testa)"]];
    
    [self addWebViewFooter];
    
    
}
#pragma mark - 内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if ([self isViewLoaded] && self.view.window == nil) {
        
        self.view = nil;
    }
}

@end
